package com.elean.springboot.superprint.autoconfigure;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.elean.springboot.superprint.autoconfigure.util.SignUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @program: elean-superprint-springboot-starter
 * @className: SuperprintUtil
 * @description:
 * @author: cxb
 * @create: 2023-03-01 16:26
 **/
public class SuperprintUtil {
    @Resource
    SuperprintProperties superprintProperties;
    @Resource
    RestTemplate restTemplate;
    public SpResult sendOrder(SendOrderDto sendOrderDto){
        String res = SignUtil.getSignStr(sendOrderDto,superprintProperties.getSecuret());
        sendOrderDto.setSignStr(res);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(JSON.toJSONString(sendOrderDto), headers);
        String result= restTemplate.postForEntity(superprintProperties.getSendOrderUrl(),formEntity,String.class).getBody();
        return JSONObject.parseObject(result, SpResult.class);
    }
}
